<template>
    <div class="page">
        <div class="top">
            <div class="top-card">
                <custom-nav-bar :text="'健康综合分析'" :bgColor="'tansparent'" :frontColor="'#432F37'" :isFixed="false"
                    isPlaceholder />
                <div class="card">
                    <image src="https://andy-res.oss-cn-chengdu.aliyuncs.com/meyka-miniapp/static/heltyh_empty_1.png" class="no-data-img" />
                    <image src="https://andy-res.oss-cn-chengdu.aliyuncs.com/meyka-miniapp/static/heltyh_empty_2.png" class="banner" />
                </div>                
            </div>
            <div class="contact">
                    <div class="contact_top">
                        <div class="name">
                            <image src="https://andy-res.oss-cn-chengdu.aliyuncs.com/meyka-miniapp/static/heltyh_empty_3.png" class="name_icon" />
                            <div class="name_text">{{  curStore && curStore.storeName || '-'}}</div>
                        </div>
                        <div class="tel_box" @click="callTel">
                            <image src="https://andy-res.oss-cn-chengdu.aliyuncs.com/meyka-miniapp/static/heltyh_empty_4.png" class="tel_icon" />
                            <div class="tel">电话咨询</div>
                        </div>
                    </div>
                    <div class="bottom">
                        <div class="status">营业中</div>
                        <div class="time">营业时间：{{curStore && curStore.openingHours || '-'}}</div>
                    </div>
                </div>
                <div class="adr" @click="goMap">
                    <div class="left">
                        <div class="num">距离{{ distance }}</div>
                        <div class="adr_text">{{curStore && curStore.address || '-'}}</div>
                    </div>
                    <div class="right">
                        <image class="map_icon" src="https://andy-res.oss-cn-chengdu.aliyuncs.com/meyka-miniapp/static/heltyh_empty_5.png" />
                        <div class="nav_text">导航</div>
                    </div>
                </div>
                <div class="btn" @click="callTel"> 立即预约体测</div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import api from '@/service';
import { getDistance } from '@/utils';
import useLogin from "@/functions/useLogin";

const loginS = useLogin();

interface Store {
    storeName: string;
    address: string;
    cityId: string;
    cityName: string;
    coordinates: string;
    desc: string;
    districtId: string;
    districtName: string;
    fullAddress: string;
    openingHours: string;
    phoneNumber: string;
    provinceId: string;
    provinceName: string;
    status: string;
    storeCode: string;
    storeCover: string;
    [key: string]: any;
}

const storeList = ref<Store[]>([]);
const curStore = computed(() => {
    return storeList.value[storeList.value.length - 1];
});

onMounted(() => {
    init();    
});

const distance = ref<string | number>('-');

const init = async () => {    
    const res = await api.store.getStoreList();
    if (res && res.status === 200) {
        storeList.value = res.data.stores;
        getLocation();
    }
};

const callTel =async () => {
    const flag = await loginS.checkLogin();
    if(flag){
        uni.makePhoneCall({
            phoneNumber: curStore.value.phoneNumber
        });
    }    
};  

const goMap = async() => {
    const [latitude, longitude] = curStore.value.coordinates.split(',');
    const flag = await loginS.checkLogin();
    if(flag){
        uni.openLocation({
            latitude: Number(latitude),
            longitude: Number(longitude),
            name: curStore.value.storeName,
            address: curStore.value.address
        });
    }    
};

const getLocation = () => {
    const [latitude2, longitude2] = curStore.value.coordinates.split(',');


    uni.authorize({
      scope: 'scope.userFuzzyLocation', // 指定需要授权的范围为用户位置信息
      success: () => {
        // 如果用户已授权或成功授权
        uni.getFuzzyLocation({
            type: 'wgs84',
            success (res) {
                const latitude = res.latitude
                const longitude = res.longitude
                const _distance = getDistance(latitude, longitude, Number(latitude2), Number(longitude2))
                console.log(_distance)
                distance.value = _distance
            },
            fail (err) {
                console.log(err)
            }
        })
      },
      fail: () => {
        // 如果用户未授权或拒绝授权
        uni.showModal({
          title: '权限提示', // 弹窗标题
          content: '需要获取您的位置信息才能为您推荐最近的门店，请前往设置开启权限', // 弹窗内容
          success: (res) => {
            // 弹窗关闭后的回调
            if (res.confirm) {
              // 如果用户点击了“确定”按钮
              uni.openSetting(); // 打开设置页面，让用户手动开启权限
            }
          }
        });
      }
    });

    
}

</script>
<style scoped lang="scss">
.page {
    background: #F0F3F7;

    .top {
        border-radius: 0rpx 0rpx 60rpx 60rpx;
        box-sizing: border-box;

        .top-card {
            background: #DEDAF9;
            padding: 0 24rpx 24rpx 24rpx;
            border-radius: 0 0 50rpx 50rpx;
            box-sizing: border-box;
            .card {
                .no-data-img {
                    width: 726rpx;
                    height: 252rpx;
                }

                .banner {
                    width: 702rpx;
                    height: 396rpx;
                    border-radius: 24rpx;
                }
            }
        }
        .contact {
                width: 704rpx;
                margin: 0 auto;
                margin-top: 60rpx;
                box-sizing: border-box;

                .contact_top {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    padding-bottom: 18rpx;
                    box-sizing: border-box;

                    .name {
                        display: flex;
                        align-items: center;
                        font-size: 32rpx;
                        font-weight: 600;
                        line-height: 44rpx;

                        .name_icon {
                            width: 36rpx;
                            height: 36rpx;
                            margin-right: 10rpx;
                        }

                        .name_text {
                            font-weight: 600;
                            font-size: 32rpx;
                            color: #333333;
                            line-height: 44rpx;
                        }
                    }

                    .tel_box {
                        display: flex;
                        align-items: center;
                        padding: 8rpx 24rpx;
                        background: #7966B3;
                        border-radius: 28rpx;
                        box-sizing: border-box;

                        .tel_icon {
                            width: 28rpx;
                            height: 28rpx;
                            margin-right: 10rpx;
                        }

                        .tel {
                            font-size: 28rpx;
                            color: #fff;
                            line-height: 40rpx;
                        }
                    }

                }

                .bottom {
                    display: flex;
                    align-items: center;
                    padding-bottom: 60rpx;
                    border-bottom: 1px solid #C4D1E3;
                    box-sizing: border-box;

                    .status {
                        position: relative;
                        font-size: 24rpx;
                        color: #999;
                        padding-right: 20rpx;
                        margin-right: 20rpx;
                        flex-shrink: 0;
                        &::after{
                            content: '';
                            position: absolute;
                            right: 0;
                            top: 50%;
                            transform: translateY(-50%);
                            width: 1px;
                            height: 24rpx;
                            background:  #C8CED6;;
                        }
                    }

                    .time {
                        font-family: PingFangSC, PingFang SC;
                        font-weight: 400;
                        font-size: 24rpx;
                        color: #333333;
                        line-height: 34rpx;
                        text-align: left;
                        font-style: normal;
                    }
                }
            }

            .adr {
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 24rpx;
                box-sizing: border-box;

                .left {
                    padding-right: 24rpx;
                    box-sizing: border-box;

                    .num {
                        display: inline-flex;
                        font-size: 24rpx;
                        color: #999;
                        background: #FFFFFF;
                        border-radius: 8rpx;
                        line-height: 34rpx;
                        padding: 0 10rpx;
                        margin-right: 20rpx;
                        border: 1px solid #CBCBCB;
                        box-sizing: border-box;
                    }

                    .adr_text {
                        display: inline;
                        font-size: 24rpx;
                        color: #333333;
                        line-height: 34rpx;
                        text-align: left;
                        font-style: normal;
                    }
                }

                .right {
                    .map_icon {
                        width: 60rpx;
                        height: 60rpx;
                    }

                    .nav_text {
                        font-size: 24rpx;
                        color: #333333;
                        line-height: 34rpx;
                    }
                }
            }

            .btn {
                margin: 0 auto;
                width: 638rpx;
                background: #7966B3;
                border-radius: 12rpx;
                font-weight: 400;
                font-size: 32rpx;
                color: #FFFFFF;
                line-height: 44rpx;
                padding: 22rpx 0;
                margin-top: 50rpx;
                text-align: center;
            }

    }
}
</style>